from fastapi import APIRouter, Query, Depends
from pydantic import BaseModel, Field
from sqlalchemy import text
from sqlalchemy.orm import Session

from commons.BaseResponse import BaseResponse
from datebase import get_db

data_phone_page_router = APIRouter(prefix="/phonePage", tags=["phonePage"])


class AirSearch(BaseModel):
    boxId: str = Field(None, description="编号")
    fpointNo: str = Field(None, description="站点编号")


@data_phone_page_router.get("/getAirDataByBoxId", summary="app.新风当前数据")
def getAirDataByBoxId(air: AirSearch = Query(), db: Session = Depends(get_db)):
    sql = "select fwd,fsd,fmoniter_Pm25,fmoniter_Pm10,fmoniter_Co2,fmoniter_Jq,fmoniter_Yjw,fupddate ftime"
    sql = sql + " from a_tc_point_moniter"
    sql = sql + " where to_char(fupddate,'yyyy-mm-dd')=to_char(now(),'yyyy-mm-dd')"
    sql = sql + " and fmacno= :boxId "
    sql = sql + " order by fupddate desc"
    sql = sql + " limit 5"
    data = db.execute(text(sql), {
        "boxId": air.boxId,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['t'] = data[key][0]
        d1['h'] = data[key][1]
        d1['pm25'] = data[key][2]
        d1['pm10'] = data[key][3]
        d1['co2'] = data[key][4]
        d1['hcho'] = data[key][5]
        d1['tvoc'] = data[key][6]
        d1['time'] = data[key][7]
        d1['fhour'] = None
        d1['id'] = None
        d1['name'] = None
        d1['lat'] = None
        d1['lon'] = None
        d1['pm1'] = None
        d1['co'] = None
        d1['utcTime'] = None
        d1['org'] = None
        d1['area'] = None
        d1['type'] = None
        d1['odm'] = None
        d1['status'] = None
        d1['adfStatus'] = None
        d1['o3'] = None
        d1['no2'] = None
        d1['so2'] = None
        d1['feuipmenttype'] = None
        d1['ftime'] = None
        d1['ftime2'] = None
        d1['fequipmentno'] = None
        d1['fequipmentname'] = None
        d1['fpointName'] = None
        d1['noise'] = None
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@data_phone_page_router.get("/getAirHourDataByBoxId", summary="app.新风24小时数据")
def dogetAirHourDataByBoxId(air: AirSearch = Query(), db: Session = Depends(get_db)):
    sql = "select cast(avg(fwd) as decimal(4,1)) fwd,cast(avg(fsd) as decimal(5,2)) fsd"
    sql = sql + " ,cast(avg(fmoniter_Pm25) as decimal(9,3)) pm25"
    sql = sql + " ,cast(avg(fmoniter_Pm10) as decimal(9,3)) pm10"
    sql = sql + " ,cast(avg(fmoniter_Co2) as decimal(5,0)) co2"
    sql = sql + " ,cast(avg(fmoniter_Jq) as decimal(5,3)) jq"
    sql = sql + " ,cast(avg(fmoniter_Yjw) as decimal(5,3)) yjw"
    sql = sql + " ,date_part('hour',fupddate) ftime"
    sql = sql + " from a_tc_point_moniter"
    sql = sql + " where fupddate between to_timestamp(to_char(now()+interval '-5 hour','yyyy-mm-dd hh24:00:01'),'yyyy-mm-dd hh24:mi:ss')"
    sql = sql + " and to_timestamp(to_char(now()+interval '0 hour','yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss')"
    sql = sql + " and fpoint_no= :fpointNo "
    sql = sql + " group by date_part('hour',fupddate)"
    data = db.execute(text(sql), {
        "fpointNo": air.fpointNo,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['t'] = data[key][0]
        d1['h'] = data[key][1]
        d1['pm25'] = data[key][2]
        d1['pm10'] = data[key][3]
        d1['co2'] = data[key][4]
        d1['hcho'] = data[key][5]
        d1['tvoc'] = data[key][6]
        d1['fhour'] = data[key][7]
        d1['ftime'] = None
        d1['id'] = None
        d1['name'] = None
        d1['lat'] = None
        d1['lon'] = None
        d1['pm1'] = None
        d1['co'] = None
        d1['utcTime'] = None
        d1['org'] = None
        d1['area'] = None
        d1['type'] = None
        d1['odm'] = None
        d1['status'] = None
        d1['adfStatus'] = None
        d1['o3'] = None
        d1['no2'] = None
        d1['so2'] = None
        d1['feuipmenttype'] = None
        d1['ftime'] = None
        d1['ftime2'] = None
        d1['fequipmentno'] = None
        d1['fequipmentname'] = None
        d1['fpointName'] = None
        d1['noise'] = None
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)


@data_phone_page_router.get("/getAirDayDataByBoxId", summary="app.新风本周数据")
def dogetAirDayDataByBoxId(air: AirSearch = Query(), db: Session = Depends(get_db)):
    sql = "select fwd,fsd,fmoniter_Pm25,fmoniter_Pm10,fmoniter_Co2,fmoniter_Jq,fmoniter_Yjw "
    sql = sql + " ,to_char(fmoniter_date,'mm-dd') ftime"
    sql = sql + " from a_td_point_moniter"
    sql = sql + " where fmoniter_date between to_timestamp(to_char(now()+interval '-5 day','yyyy-mm-dd'),'yyyy-mm-dd')"
    sql = sql + " and to_timestamp(to_char(now()+interval '0 day','yyyy-mm-dd'),'yyyy-mm-dd')"
    sql = sql + " and fpoint_no= :fpointNo "
    data = db.execute(text(sql), {
        "fpointNo": air.fpointNo,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['t'] = data[key][0]
        d1['h'] = data[key][1]
        d1['pm25'] = data[key][2]
        d1['pm10'] = data[key][3]
        d1['co2'] = data[key][4]
        d1['hcho'] = data[key][5]
        d1['tvoc'] = data[key][6]
        d1['fhour'] = data[key][7]
        d1['ftime'] = None
        d1['id'] = None
        d1['name'] = None
        d1['lat'] = None
        d1['lon'] = None
        d1['pm1'] = None
        d1['co'] = None
        d1['utcTime'] = None
        d1['org'] = None
        d1['area'] = None
        d1['type'] = None
        d1['odm'] = None
        d1['status'] = None
        d1['adfStatus'] = None
        d1['o3'] = None
        d1['no2'] = None
        d1['so2'] = None
        d1['feuipmenttype'] = None
        d1['ftime'] = None
        d1['ftime2'] = None
        d1['fequipmentno'] = None
        d1['fequipmentname'] = None
        d1['fpointName'] = None
        d1['noise'] = None
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
